//
//  KABCosActiviyCCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/6/19.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosActiviyCCell.h"
#import "KABModel.h"
#import <YYWebImage.h>
#import "UIColor+XDHex.h"
#import "GlobalHeader.h"

@interface KABCosActiviyCCell ()
@property (nonatomic, strong) NSArray<UIImage *> *indexImgArrays;
@end

@implementation KABCosActiviyCCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.portraitImgView.layer.masksToBounds = YES;
    self.portraitImgView.layer.cornerRadius = CGRectGetWidth(self.portraitImgView.bounds) / 2.0;
    
    self.voteBtn.layer.masksToBounds = YES;
    self.voteBtn.layer.cornerRadius = CGRectGetHeight(self.voteBtn.bounds) / 2.0;
    [self.voteBtn addTarget:self action:@selector(voteBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.indexImgArrays = @[[UIImage imageNamed:@"cosplay_activity_gold"],
                            [UIImage imageNamed:@"cosplay_activity_silver"],
                            [UIImage imageNamed:@"cosplay_activity_bronze"]];
}

- (void)voteBtnTapped:(UIButton *)btn {
    if (btn.selected) {
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(kABCosActiviyCCell:voteBtnTapped:)]) {
        [self.delegate kABCosActiviyCCell:self voteBtnTapped:btn];
    }
}

- (void)configViewWithCosActItem:(KABCosActivityItem *)item {
    [self.portraitImgView yy_setImageWithURL:[NSURL URLWithString:item.user_headimg_url]
                                 placeholder:[KABDefaultVarModel sharedInstance].smallDefaultImg];
    UIImage *rankBgImg = nil;
    NSString *rankStr = nil;
    if (item.ranking <= 1000) {
        rankBgImg = self.indexImgArrays[0];
        rankStr = [NSString stringWithFormat:@"%zd", item.ranking];
    } else if (item.ranking <= 5000) {
        rankBgImg = self.indexImgArrays[1];
    } else {
        rankBgImg = self.indexImgArrays[2];
    }
    [self.rankBtn setBackgroundImage:rankBgImg forState:UIControlStateNormal];
    [self.rankBtn setTitle:rankStr forState:UIControlStateNormal];
    
    self.nameLabel.text = item.user_nickname;
    [self.ticketCntBtn setTitle:[NSString stringWithFormat:@" %zd", item.ballot] forState:UIControlStateNormal];
    
    self.voteBtn.selected = item.is_voted;
    self.voteBtn.backgroundColor = self.voteBtn.selected ? [UIColor xd_colorWithHex:0xD8D8D8] : [UIColor xd_colorWithHex:KABGlobalColorRedBtnBorderColor];
}

@end
